package com.mjlf.project.web.unlogin;

import com.mjlf.framework.annotation.Autowired;
import com.mjlf.framework.annotation.mvc.Controller;
import com.mjlf.framework.annotation.mvc.RequestMapping;
import com.mjlf.framework.mvc.eneity.ReqParams;
import com.mjlf.project.entity.User;
import com.mjlf.project.service.LoginService;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;


@Controller
@RequestMapping("/mjlf")
public class LoginController {
    Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private LoginService loginService;

    @RequestMapping(value = "/login", method = "POST")
    public void login(HttpServletRequest request, HttpServletResponse response, ReqParams params) throws IOException {
        JSONObject jsonObject = null;

        String username = params.getParam("username");
        String password = params.getParam("password");

        User user = new User();
        user.setPassword(password);
        user.setUsername(username);

        if(loginService.login(user)){
            jsonObject = new JSONObject();
            jsonObject.put("loginStatus", "yes");
        }else {
            jsonObject = new JSONObject();
            jsonObject.put("loginStatus", "no");
        }

        response.getWriter().write(jsonObject.toString());
    }

    @RequestMapping(value = "/login1", method = "POST")
    public JSONObject login1(HttpServletRequest request, HttpServletResponse response) throws IOException {
        JSONObject jsonObject = null;
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        if(username.equals("mjlf") && password.equals("123456")){
            jsonObject = new JSONObject();
            jsonObject.put("loginStatus", "yes");
        }
        return jsonObject;
    }
}
